package com.jason.kotlin.gank.ui

import android.text.TextUtils
import android.widget.Button
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jason.kotlin.gank.R
import com.jason.kotlin.gank.model.RollAimBean
import com.jason.kotlin.gank.ui.adapter.RollRubbishAdapter
import com.jason.kotlin.gank.viewmodel.RollMoreViewModel
import com.jason.library.common.base.BaseStateActivity
import com.jason.library.common.ext.observe
import com.jason.library.common.util.KeyboardUtils
import com.jason.library.widget.edittext.MClearEditText
import com.jason.library.widget.snackbar.MySnackbar

/**
 * @description: RollRubbishActivity
 *
 * @author ChenBaoyang
 * @date 4/25/21 10:15
 */
class RollRubbishActivity : BaseStateActivity<RollMoreViewModel>(){

    private lateinit var queryBtn: Button
    private lateinit var rubbishEdit: MClearEditText
    private lateinit var recyclerView: RecyclerView

    private lateinit var adapter: RollRubbishAdapter

    override fun bindLiveData() {
        observe(viewModel!!.dataRubbish) {
            val datas = mutableListOf<RollAimBean>()
            datas.add(it.aim)
            datas.addAll(it.recommendList)
            adapter.setNewData(datas)
        }
    }

    override fun getPageTarget() = recyclerView

    override fun initView() {
        title = "垃圾分类"
        queryBtn = findViewById(R.id.btn_query)
        rubbishEdit = findViewById(R.id.edit_text_rubbish)
        recyclerView = findViewById(R.id.recyclerView)

        adapter = RollRubbishAdapter()
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        recyclerView.itemAnimator = DefaultItemAnimator()
        recyclerView.addOnScrollListener(object :
            RecyclerView.OnScrollListener() {
            override fun onScrolled(
                recyclerView: RecyclerView,
                dx: Int,
                dy: Int
            ) {
                if (Math.abs(dy) > 20) {
                    KeyboardUtils.hideInputKeyboard(this@RollRubbishActivity)
                }
                super.onScrolled(recyclerView, dx, dy)
            }
        })

        queryBtn.setOnClickListener {
            //获取关键字
            //获取关键字
            val keyWord = rubbishEdit.getText().toString()
            if (TextUtils.isEmpty(keyWord)) {
                MySnackbar.makeSnackBarBlack(toolbar, "查询内容不能为空")
                return@setOnClickListener
            }
            KeyboardUtils.hideInputKeyboard(this@RollRubbishActivity)

            viewModel!!.getRubbishType(keyWord)
        }
    }

    override fun reload() {

    }

    override fun getLayoutId() = R.layout.roll_activity_rubbish

    override fun onDataInit() {

    }
}